<template>
    <div class="bought-list" :class="[{'empty':showEmpty}]">
        <template v-if="!isLoadDb">
            <page-loading></page-loading>
        </template>
        <template v-else>
            <div v-if="showEmpty" class="div-empty">
                <span class="iconfont icon-kong"></span>
                <p class="empty-tip">您还未购买过商品~</p> 
            </div>
            <ul v-else class="meal-content" v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="0" infinite-scroll-immediate-check="false">
                <li v-for="dataItem in goodsArr" :goodId="dataItem.buyid" @click="cellClickHandle">
                    <meal-cell :goods="dataItem" class="meal-cell">
                    </meal-cell>
                </li>
            </ul>
        </template>
    </div>
</template>

<script>
import pageLoading from "../../loading/pageLoading.vue";
import Vue from "vue";
import { mapState } from "vuex";
import { formatDate } from "../../../common/js/date.js";
import mealCell from "./mealCell.vue";
import { InfiniteScroll } from "mint-ui";
Vue.use(InfiniteScroll);

export default {
  name: "bought-list",
  data() {
    return {
      isLoadDb:false,
      goodsArr: [],
      showEmpty: false,
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [] //保存已经显示的订单id
    };
  },
  components: {
    mealCell,pageLoading
  },
  mounted() {
    this.showEmpty = false;
    this.requestData(false);
  },
  methods: {
    cellClickHandle(event) {
      let curTarget = event.currentTarget;
      let goodId = curTarget.getAttribute("goodId");
      this.$router.push({ path: "/mealTicketList/" + goodId });
    },
    requestData(infinite = true) {
      if (infinite && this.page == 0) return;
      if (infinite && this.cellLoading) return;
      this.cellLoading = true;
      this.$http
        .post(this.path + `store/Goods/getBuyList`, {
          uid: this.userid,
          page: this.curPage
        })
        .then(response => {
          this.setLoadDb();
          let serverData = response.body.result;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0) this.showEmpty = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].buyid;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.goodsArr.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        let tempDate = new Date(val.buydate * 1000);
        let buyTime = formatDate(tempDate, "yyyy-MM-dd");
        detailsArr.push({
          id: val.id.toString(),
          buyid: val.buyid.toString(),
          names: val.setsname,
          url: val.icon,
          buytm: buyTime,
          passtm: buyTime,
          price: val.price,
          // dec: val.message,
          dec: '全能补水王，点滴承若，极简即奢,全能补水王，点滴承若，极简即奢',
          hadUesed: val.orderstate.toString() == "2"
        });
      });
      return detailsArr;
    },
    setLoadDb() {
      if (this.isLoadDb) return;
      let clearId = setTimeout(() => {
        clearTimeout(clearId);
        this.isLoadDb = true;
      }, 200);
    }
  },
  computed: {
    ...mapState(["path", "userid", "role"])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.bought-list {
  width: 100%;
  min-height: 100%;
  background-color: #efefef;

  &.empty {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    background-color: #fff;
  }

  .meal-content {
    width: 100%;

    li {
      width: 100%;
      background-color: #fff;
      margin-bottom: 0.07rem;

      .meal-cell {
        width: 100%;
      }
    }
  }

  .div-empty {
    margin-bottom 25vh;
    .icon-kong {
      font-size: 1.2rem;
      color: #595656;
    }

    .empty-tip {
      margin-top: 0.26rem;
      font-size: 0.13rem;
      text-align: center;
      color: #595656;
    }
  }
}
</style>
